import { TOKEN_NAME } from "@/config";
import store from "@/store";
import jwt_decode from "jwt-decode";
import { getStorage, setStorage } from "./utils";

export default (dispatch: any) => {
  let jwt = getStorage(TOKEN_NAME) || "";

  if (jwt) {
    let token: any = null;
    token = jwt_decode(jwt);

    !!token && dispatch({ type: "SET_USER_LOGINR", payload: token });
    const {
      user: { permissions },
    } = store.getState().userInfo;
    setStorage("ROLE", permissions);
  }
};
